package com.xc.freeapp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.xc.freeapp.dao.IssueDao;
import com.xc.freeapp.dao.RelationDao;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.Issue;
import com.xc.freeapp.entity.Relation;
import com.xc.freeapp.request.bean.RelationAndIssue;
import com.xc.freeapp.service.RelationService;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.PropertiesUtil;
import com.xc.freeapp.util.UploadUtil;

@Service
public class RelationServiceImpl implements RelationService {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private RelationDao relationDao;

	@Autowired
	private IssueDao issueDao;

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
	public int deleteByPrimaryKey(Integer id) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("删除关系表以及就诊记录数据");
		int a = relationDao.deleteByPrimaryKey(id);
		issueDao.deleteByRelationId(id);// 删除就诊记录
		return a;
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int insert(Relation record) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("插入关系表数据");
		return relationDao.insert(record);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public Relation selectByPrimaryKey(Integer id) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("根据主键获取记录");
		return relationDao.selectByPrimaryKey(id);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateByPrimaryKey(Relation record) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("更新表数据");
		return relationDao.updateByPrimaryKey(record);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Relation> getList(Map<String, Object> map,PageBounds pageBounds) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("根据分组查询关系列表");
		return relationDao.getList(map,pageBounds);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public Map saveRelationAndIssue(RelationAndIssue relationAndIssue,String userid) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String,Object> resultMap=new HashMap<String, Object>();
		Relation relation = relationAndIssue.getRelation();
		Issue issue = relationAndIssue.getIssue();
		log.info("保存关系和就诊记录");
		if (relation.getId() != null && relation.getId() > 0) {// 修改
			relation.setUpdateby(Integer.parseInt(userid));
			issue.setUpdateby(Integer.parseInt(userid));
			relationDao.updateByPrimaryKey(relation);
			issueDao.updateByPrimaryKey(issue);
		} else {// 保存
			relation.setCreateby(Integer.parseInt(userid));
			relation.setUpdateby(Integer.parseInt(userid));
			issue.setCreateby(Integer.parseInt(userid));
			issue.setUpdateby(Integer.parseInt(userid));
			relation.setUserid(Integer.parseInt(userid));
			relationDao.insert(relation);
			issue.setRelationid(relation.getId());
			issueDao.insert(issue);
		}
		Relation relationRet = this.selectByPrimaryKey(relation.getId());

		Issue issueRet = issueDao.selectByPrimaryKey(issue.getId());
		

		resultMap.put("Relation", relationRet);
		resultMap.put("Issue", issueRet);
	
		return resultMap;
	}

}
